﻿using Abp.Authorization;
using Abp.UI;
using Paas.App.Interface.Approvals;
using Paas.App.Interface.Approvals.Dtos;
using Paas.App.Interface.Approvals.Messages;
using Paas.Core;
using Paas.Core.Approvals.Entities;
using Paas.Core.Sys.Entities;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Basefr.App;
using System;
using System.Linq;

namespace Paas.App.Approvals
{
    [Route("Approve")]
    [AbpAuthorize()]
    public class ApproveAppService : AppService, IApproveAppService
    {
        private readonly IMainRepository<ApproveTemplate> _templateRepo;
        private readonly IMainRepository<Approve> _approveRepo;
        private readonly IMainRepository<User, long> _userRepo;

        public ApproveAppService(IMainRepository<ApproveTemplate> templateRepo, IMainRepository<Approve> approveRepo, IMainRepository<User, long> userRepo)
        {
            _templateRepo = templateRepo;
            _approveRepo = approveRepo;
            _userRepo = userRepo;
        }

        [HttpPost(nameof(GetTemplate))]
        public virtual GetTemplateOutput GetTemplate(GetTemplateInput input)
        {
            var template = _templateRepo.GetAll()
                .Include(p => p.Steps).ThenInclude(p => p.Operators).ThenInclude(p => p.Operator)
                .FirstOrDefault(p => p.Type == input.Type);

            if (template == null || template.Steps.Count < 2)
            {
                return new GetTemplateOutput { HasConfig = false };
            }

            //var roleIds = template.Steps.SelectMany(p => p.Operators.Select(r => r.OperatorId)).ToList();

            //var approvers = _userRepo.GetAll()
            //    .Include(p => p.Roles).ThenInclude(p => p.Role)
            //    .Where(p => p.Roles.Any(r => roleIds.Contains(r.RoleId))).ToList();

            return new GetTemplateOutput { Item = ObjectMapper.Map<ApproveTemplateDto>(template), HasConfig = true };
        }

        [HttpPost(nameof(GetApprove))]
        public virtual GetApproveOutput GetApprove(GetApproveInput input)
        {
            var approve = _approveRepo.GetAll()
                .Include(p => p.Steps).ThenInclude(p => p.Operator)
                .Include(p => p.Template.Steps).ThenInclude(p => p.Operators).ThenInclude(p => p.Operator)
                .OrderByDescending(p => p.Id)
                .FirstOrDefault(p => p.Template.Type == input.Type && p.ObjectId == input.ObjectId);

            if (approve == null)
            {
                throw new UserFriendlyException("审批记录不存在");
            }

            return new GetApproveOutput { Item = ObjectMapper.Map<ApproveDto>(approve) };
        }

        [HttpPost(nameof(CreateApprove))]
        public virtual CreateApproveOutput CreateApprove(CreateApproveInput input)
        {
            throw new NotImplementedException();
        }

        [HttpPost(nameof(ApproveFlow))]
        public virtual ApproveFlowOutput ApproveFlow(ApproveFlowInput input)
        {
            throw new NotImplementedException();
        }
    }
}

